package com.lvmama.pangolin.timer.processor;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;





import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.lvmama.pangolin.busi.config.ConfigManager;
import com.lvmama.pangolin.busi.timer.TimerManager;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.EarlyWarning;
import com.lvmama.pangolin.entity.WarningConfig;
import com.lvmama.pangolin.service.EarlyWarningService;
import com.lvmama.pangolin.timer.CommonJob;
import com.lvmama.pangolin.util.SpringContextUtil;

/**
 * 
* @ClassName: GrabLogMonitor 
* @Description: 日志分析告警
* @author yuanchi
* @date 2017年3月6日 上午11:54:33
 */
public class GrabLogMonitor extends CommonJob {

	@Autowired
	private EarlyWarningService earlyWarningService;
	
	private Logger logger = Logger.getLogger(GrabLogMonitor.class);
	@Override
	public void runJob(JobExecutionContext context, String parameter)
	{
		try
		{
			logger.info("日志分析预警开始扫描...");
			String configParam = (String) context.getJobDetail().getJobDataMap()
					.get(TimerManager.JOB_DATAKEY);
			if (earlyWarningService == null)
			{
				earlyWarningService = (EarlyWarningService) SpringContextUtil
						.getBean(EarlyWarningService.class);
			}
			List<WarningConfig> warningList = warnConfigList(configParam);
			
			if (warningList != null && !warningList.isEmpty())
			{
				Map<String, Object> param = new HashMap<String, Object>();
				List<EarlyWarning> warnList = null;
				for (WarningConfig wconfig : warningList)
				{
					if (StringTools.isValid(wconfig.getWarn_sql()))
					{
						param.put("sql", wconfig.getCheck_sql());
						param.put("warning_threshold", wconfig.getWarn_threshold());
						param.put("warning_period", wconfig.getWarn_period());
						warnList = earlyWarningService.queryWarningLogBySql(param);
						if (warnList != null && !warnList.isEmpty())
						{
							StringBuffer sb = new StringBuffer("");
							sb.append("标题：").append(wconfig.getSubject()).append("\n");
							sb.append("内容：").append(wconfig.getWarn_content());
							StringBuffer sb_hotel=new StringBuffer("");
							StringBuffer sb_ticket=new StringBuffer("");
							int amount = 0;
							for (EarlyWarning earlyWarning : warnList)
							{
								if("酒店".equals(earlyWarning.getBusi_type())){
									sb_hotel.append("\n");
									amount += earlyWarning.getAmount();
									sb_hotel.append(" ");
									sb_hotel.append(earlyWarning.getCompany_code());
									sb_hotel.append(" : ");
									sb_hotel.append(earlyWarning.getAmount());
								}
								if("门票".equals(earlyWarning.getBusi_type())){
									sb_ticket.append("\n");
									amount += earlyWarning.getAmount();
									sb_ticket.append(" ");
									sb_ticket.append(earlyWarning.getCompany_code());
									sb_ticket.append(" : ");
									sb_ticket.append(earlyWarning.getAmount());
								}
							}
							sb.append("\n酒店："+sb_hotel);
							sb.append("\n门票："+sb_ticket);
							if(amount < wconfig.getWarn_threshold())
							{
								continue;
								
							}
							sb.append("\n");
							sb.append(" 异常总数 : " + amount);
							sb.append("\n");
							sb.append(" 报警阈值 : " + wconfig.getWarn_threshold());
							String content = sb.toString();
							//处理预警信息
							earlyWarningService.DealEarlyWarning(wconfig, content);
							
						}
					}
				}
			}
			logger.info("抓取日志分析预警完成扫描......");
		}
		catch (Exception e)
		{
			logger.error("抓取日子预警分析失败!", e);
		}
		
	}

	private List<WarningConfig> warnConfigList(String configName)
	{
		ConfigManager config = ConfigManager.getInstance();
		String[] paramNameArr = config.getParamNamesByConfigName(configName);
		List<WarningConfig> warningList = new ArrayList<WarningConfig>();
		if (paramNameArr != null && paramNameArr.length > 0)
		{
			WarningConfig warnConf = null;
			String warnConfigName = "";
			warnConfigName = config.getParamValue(configName, "common");
			String commonInterval = config.getParamValue(warnConfigName,
					"warning_interval");
			String commonContent = config
					.getParamValue(warnConfigName, "warning_content");
			String commonEmail = config.getParamValue(warnConfigName, "contact_email");
			String commonSms = config.getParamValue(warnConfigName, "contact_sms");
			String commonLevel = config.getParamValue(warnConfigName, "warning_level");
			String commonPeriod = config.getParamValue(warnConfigName, "warning_period");
			String commonSubject = config.getParamValue(warnConfigName, "subject");
			String commonThreshold = config.getParamValue(warnConfigName,
					"warn_threshold");
			for (String paramName : paramNameArr)
			{
				if (!"common".equals(paramName))
				{
					warnConf = new WarningConfig();
					warnConfigName = config.getParamValue(configName, paramName);
					warnConf.setWarn_sql(config.getParamValue(warnConfigName,
							"warning_sql", ""));
					warnConf.setCheck_sql(config.getParamValue(warnConfigName,
							"check_sql", ""));
					warnConf.setWarn_interval(config.getParamValue(warnConfigName,
							"warning_interval", commonInterval));
					warnConf.setWarn_content(config.getParamValue(warnConfigName,
							"warning_content", commonContent));
					warnConf.setContact_email(config.getParamValue(warnConfigName,
							"contact_email", commonEmail));
					warnConf.setContact_sms(config.getParamValue(warnConfigName,
							"contact_sms", commonSms));
					warnConf.setWarn_level(config.getParamValue(warnConfigName,
							"warning_level", commonLevel));
					warnConf.setWarn_period(config.getParamValue(warnConfigName,
							"warning_period", commonPeriod));
					warnConf.setSubject(config.getParamValue(warnConfigName,
							"subject", commonSubject));
					warnConf.setWarn_threshold(Integer.valueOf(config.getParamValue(
							warnConfigName, "warn_threshold", commonThreshold)));
					warnConf.setWarn_code(config.getParamValue(
							warnConfigName, "warning_code"));
					warningList.add(warnConf);
				}
			}
		}
		
		return warningList;
	}
	
}
